package com.fcscs.cservice.client.main.folio;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.ReservationServiceController;
import com.fcscs.cservice.client.common.model.reservation.ReservationViewModelData;
import com.fcscs.cservice.client.main.reservation.FolioDialog;
import com.fcscs.cservice.domain.model.FolioView;
import com.fcscs.cservice.domain.model.ReservationConfView;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class FolioGrid extends PagingSearchResultsPanel<ReservationViewModelData, MainPaginationSearchModel> {
	private static FolioGrid INSTANCE;
	private FolioDialog foliodialog;
	private ReservationConfView reservConf;
	
	public static FolioGrid getINSTANCE(String title,
			SearchExecutor<ReservationViewModelData, MainPaginationSearchModel> exe){
		if(null == INSTANCE){
			INSTANCE = new FolioGrid(title, exe);
		}
		return INSTANCE;
	}
	
	public FolioGrid(
			String title,
			SearchExecutor<ReservationViewModelData, MainPaginationSearchModel> exe) {
		super(title, exe, (Object[]) null);
		setCollapsible(false);
		setSize(950, 340);
	}

	@Override
	protected List<ColumnConfig> getColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cc = CoreUtil.createNonSortableColumnConf
		(ShareGWTConstants.ROOM,ShareGWTConstants.SHARE_GWT_CONSTANTS.room(), 120);
		columns.add(cc);
		
		columns.add(CoreUtil.createNonSortableColumnConf
				(ShareGWTConstants.GUEST_NAME,ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName(), 120));
		
		columns.add(CoreUtil.createNonSortableColumnConf
				(ShareGWTConstants.ARRIVAL_DATE,ShareGWTConstants.SHARE_GWT_CONSTANTS.arrivalDate(), 120));
		columns.add(CoreUtil.createNonSortableColumnConf
				(ShareGWTConstants.DEPARTUAL_DATE,ShareGWTConstants.SHARE_GWT_CONSTANTS.departureDate(), 120));
		
		columns.add(CoreUtil.createNonSortableColumnConf
				(CServiceConstants.RESERVATION_STATUS,CServiceConstants.CSERVICE_CONSTANTS.reservationStatus(), 120));
		
		cc = CoreUtil.createNonSortableColumnConf(
				CServiceConstants.FOLIO,CServiceConstants.CSERVICE_CONSTANTS.folio(), 120);
		cc.setRenderer(new GridCellRenderer<ReservationViewModelData>() {
			public Object render(final ReservationViewModelData model,String property, ColumnData config, int rowIndex,
					int colIndex, ListStore<ReservationViewModelData> store,final Grid<ReservationViewModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setId("FolioGrid_show_folio_hr");
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = createShowFolioDialog(model);
				ib.setStyleAttribute("marginLeft", "3px");
				ib.setId("FolioGrid_show_folio_button");
				hr.add(ib);
				return hr;
			}			
		});
		columns.add(cc);
		
		return columns;
	}
	
	private HighLightIconButton createShowFolioDialog(final ReservationViewModelData model){
		HighLightIconButton iconBtn = new HighLightIconButton(CServiceConstants.CSS_BILL_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						if(model.getEntity().getFolioId() != null){
						ReservationServiceController.getINSTANCE().getFolioView(
								model.getEntity().getFolioId(), 
								new AsyncCallbackListener<FolioView>(){
									@Override
									public void onLogin(String username) {
										ReservationServiceController.getINSTANCE().getFolioView(
												model.getEntity().getFolioId(),this);
									}

									@Override
									public void onFailure(Throwable caught) {
										MessageBox.alert(
												CoreConstants.CORE_MESSAGES.operation(
													CoreConstants.CORE_CONSTANTS.system(), 
													ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
												CoreConstants.CORE_CONSTANTS.systemError(), 
												null);
									}

									@Override
									public void onSuccess(FolioView result) {
										foliodialog = FolioDialog.getINSTANCE(reservConf);
										foliodialog.getFormPanel().updateForm(result);
										foliodialog.show();
									}
									
								});
					}else{
						MessageBox.alert(
								"",
								CServiceConstants.CSERVICE_MESSAGES.thereIsNo(CServiceConstants.CSERVICE_CONSTANTS.folio()), 
								null);
					}
					}
				});
		return iconBtn;
	}

	public void setReservConf(ReservationConfView reservConf) {
		this.reservConf = reservConf;
	}

}
